home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_kdelibs.idb / usr / freeware / kde / include / htmltoken.h.z / htmltoken.h
Encoding:
C/C++ Source or Header  |  1999-01-26  |  4.9 KB  |  229 lines

  1. /* This file is part of the KDE libraries
  2.     Copyright (C) 1997 Martin Jones (mjones@kde.org)
  3.               (C) 1997 Torben Weis (weis@kde.org)
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public License
  16.     along with this library; see the file COPYING.LIB.  If not, write to
  17.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18.     Boston, MA 02111-1307, USA.
  19. */
  20. //-----------------------------------------------------------------------------
  21. //
  22. // KDE HTML Widget
  23. //
  24.  
  25. #ifndef HTMLTOKEN_H
  26. #define HTMLTOKEN_H
  27.  
  28. class StringTokenizer;
  29. class HTMLTokenizer;
  30.  
  31. #include <qlist.h>
  32. #include <qstrlist.h>
  33. #include <qarray.h>
  34.  
  35. #include "jscript.h"
  36.  
  37. // Every tag as deliverd by HTMLTokenizer starts with TAG_ESCAPE. This way
  38. // you can devide between tags and words.
  39. #define TAG_ESCAPE 13
  40.  
  41. // The count of spaces used for each tab.
  42. #define TAB_SIZE 8
  43.  
  44.  
  45.  
  46. //-----------------------------------------------------------------------------
  47.  
  48. class Token
  49. {
  50. public:
  51.     Token( const char *t, int len )
  52.     {
  53.     tok = new char [ len + 1 ];
  54.     memcpy( tok, t, len+1 );
  55.     nextToken = 0;
  56.     }
  57.  
  58.     ~Token()
  59.     {
  60.     delete [] tok;
  61.     }
  62.  
  63.     char *token()
  64.     { return tok; }
  65.  
  66.     Token *next()
  67.     { return nextToken; }
  68.     void setNext( Token *n )
  69.     { nextToken = n; }
  70.  
  71. private:
  72.     char *tok;
  73.     Token *nextToken;
  74. };
  75.  
  76. //-----------------------------------------------------------------------------
  77.  
  78. class BlockingToken
  79. {
  80. public:
  81.     enum TokenType { Table, FrameSet, Script, Cell };
  82.  
  83.     BlockingToken( TokenType tt, Token *t )
  84.         {    ttype = tt; tok = t; }
  85.  
  86.     Token *token()
  87.         {    return tok; }
  88.     const char *tokenName();
  89.  
  90. protected:
  91.     TokenType ttype;
  92.     Token *tok;
  93. };
  94.  
  95. //-----------------------------------------------------------------------------
  96.  
  97. class HTMLTokenizer
  98. {
  99. public:
  100.     HTMLTokenizer( KHTMLWidget *_widget = 0L );
  101.     ~HTMLTokenizer();
  102.  
  103.     void begin();
  104.     void write( const char * );
  105.     void end();
  106.  
  107.     char* nextToken();
  108.     bool hasMoreTokens();
  109.  
  110.     void first()
  111.     { curr = head; }
  112.  
  113. protected:
  114.     void reset();
  115.     void appendToken( const char *t, int len )
  116.     {
  117.     if ( len < 1 )
  118.         return;
  119.  
  120.     Token *tok = new Token( t, len );
  121.  
  122.     if ( head )
  123.     {
  124.         tail->setNext( tok );
  125.         tail = tok;
  126.     }
  127.     else
  128.     {
  129.         head = tail = tok;
  130.     }
  131.  
  132.     if ( !curr )
  133.         curr = tok;
  134.     }
  135.     
  136. protected:
  137. //    QStrList tokenList;
  138.     char *buffer;
  139.     char *dest;
  140.  
  141.     Token *head;
  142.     Token *tail;
  143.  
  144.     Token *curr;
  145.     
  146.     // the size of buffer
  147.     int size;
  148.     
  149.     // are we in a html tag
  150.     bool tag;
  151.  
  152.     // are we in quotes within a html tag
  153.     bool tquote;
  154.     
  155.     // To avoid multiple spaces
  156.     bool space;
  157.  
  158.     // Discard line breaks immediately after tags
  159.     bool discardCR;
  160.     
  161.     // Are we in a <pre> ... </pre> block
  162.     bool pre;
  163.     
  164.     // Are we in a <script> ... </script> block
  165.     bool script;
  166.  
  167.     // Are we in a <select> ... </select> block
  168.     bool select;
  169.  
  170.     // Area we in a <!-- comment --> block
  171.     bool comment;
  172.  
  173.     // Are we in a <textarea> ... </textarea> block
  174.     bool textarea;
  175.  
  176.     // Used to store the code of a srcipting sequence
  177.     char *scriptCode;
  178.     // Size of the script sequenze stored in @ref #scriptCode
  179.     int scriptCodeSize;
  180.     // Maximal size that can be stored in @ref #scriptCode
  181.     int scriptCodeMaxSize;
  182.     
  183.     // Used to store the string "</script>" for comparison
  184.     const char *scriptString;
  185.     // Stores characters if we are scanning for "</script>"
  186.     char scriptBuffer[ 10 ];
  187.     // Counts where we are in the string "</script>"
  188.     int scriptCount;
  189.     
  190.     // Is TRUE if we are in a <script> tag and insideof '...' quotes
  191.     bool squote;
  192.     // Is TRUE if we are in a <script> tag and insideof "..." quotes
  193.     bool dquote;
  194.  
  195.     KHTMLWidget *widget;
  196.     
  197.     /**
  198.      * This pointer is 0L until used. The @ref KHTMLWidget has an instance of
  199.      * this class for us. We ask for it when we see some JavaScript stuff for
  200.      * the first time.
  201.      */
  202.     JSEnvironment* jsEnvironment;
  203.     
  204.     // These are tokens for which we are awaiting ending tokens
  205.     QList<BlockingToken> blocking;
  206. };
  207.  
  208. //-----------------------------------------------------------------------------
  209.  
  210. class StringTokenizer
  211. {
  212. public:
  213.     StringTokenizer();
  214.     ~StringTokenizer();
  215.  
  216.     void tokenize( const char *, const char * );
  217.     const char* nextToken();
  218.     bool hasMoreTokens() { return ( pos != 0 ); }
  219.  
  220. protected:
  221.     char *pos;
  222.     char *end;
  223.     char *buffer;
  224.     int  bufLen;
  225. };
  226.  
  227. #endif // HTMLTOKEN
  228.  
  229.